/*
 * Main.java
 *
 * Created on April 4, 2007, 10:33 PM
 */

package moto9s12sim;

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
 *
 * @author  Fergus Earnshaw
 */
public class Main extends javax.swing.JFrame {
    
    /** Creates new form Main */
    public Main() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jPanel4 = new javax.swing.JPanel();
        lblRegA = new javax.swing.JLabel();
        lblRegB = new javax.swing.JLabel();
        lblRegX = new javax.swing.JLabel();
        lblRegY = new javax.swing.JLabel();
        lblRegPC = new javax.swing.JLabel();
        lblRegSP = new javax.swing.JLabel();
        lblRegCC = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        cbCC_S = new javax.swing.JCheckBox();
        cbCC_X = new javax.swing.JCheckBox();
        cbCC_H = new javax.swing.JCheckBox();
        cbCC_I = new javax.swing.JCheckBox();
        cbCC_N = new javax.swing.JCheckBox();
        cbCC_Z = new javax.swing.JCheckBox();
        cbCC_V = new javax.swing.JCheckBox();
        cbCC_C = new javax.swing.JCheckBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstMem = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstStack = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tpConsole = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnBreakPointSet = new javax.swing.JButton();
        btnBreakPointRemove = new javax.swing.JButton();
        btnStackWatchSet = new javax.swing.JButton();
        btnStackWatchRemove = new javax.swing.JButton();
        btnLoadFile = new javax.swing.JButton();
        btnSaveConsole = new javax.swing.JButton();
        btnSaveStateDump = new javax.swing.JButton();
        tfLoadFile = new javax.swing.JTextField();
        tfSaveConsole = new javax.swing.JTextField();
        tfSaveStateDump = new javax.swing.JTextField();
        btnSimReset = new javax.swing.JButton();
        btnSimStop = new javax.swing.JButton();
        btnSimStepBck = new javax.swing.JButton();
        btnSimRun = new javax.swing.JButton();
        btnSimStepFwd = new javax.swing.JButton();
        
        
        blSimState = false;//simulation not started.
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getAccessibleContext().setAccessibleDescription("null");
        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblRegA.setText("A:");
        lblRegA.setToolTipText("A -> 8-Bit Register ");

        lblRegB.setText("B:");
        lblRegB.setToolTipText("B -> 8-Bit Register ");

        lblRegX.setText("X:");
        lblRegX.setToolTipText("X -> 16-bit Register");

        lblRegY.setText("Y:");
        lblRegY.setToolTipText("X -> 16-bit Register");

        lblRegPC.setText("PC:");
        lblRegPC.setToolTipText("PC -> 16-bit Register (Program Counter)");

        lblRegSP.setText("SP:");
        lblRegSP.setToolTipText("SP -> 16-bit Register (Stack Pointer)");

        lblRegCC.setText("CC:");
        lblRegCC.setToolTipText("CC -> 8-Bit Register denoting Status Flags");

        jLabel12.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel12.setText("Registers:");

        cbCC_S.setText("CC(S) Stop Disable");
        cbCC_S.setToolTipText("CC Bit 7");
        cbCC_S.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_S.setEnabled(false);
        cbCC_S.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_X.setText("CC(X) Mask XIRQ");
        cbCC_X.setToolTipText("CC Bit 6");
        cbCC_X.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_X.setEnabled(false);
        cbCC_X.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_H.setText("CC(H) Half-Carry");
        cbCC_H.setToolTipText("CC Bit 5");
        cbCC_H.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_H.setEnabled(false);
        cbCC_H.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_I.setText("CC(I) Mask IRQ");
        cbCC_I.setToolTipText("CC Bit 4");
        cbCC_I.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_I.setEnabled(false);
        cbCC_I.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_N.setText("CC(N) Negitive");
        cbCC_N.setToolTipText("CC Bit 3");
        cbCC_N.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_N.setEnabled(false);
        cbCC_N.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_Z.setText("CC(Z) Zero");
        cbCC_Z.setToolTipText("CC Bit 2");
        cbCC_Z.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_Z.setEnabled(false);
        cbCC_Z.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_V.setText("CC(V) Overflow");
        cbCC_V.setToolTipText("CC Bit 1");
        cbCC_V.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_V.setEnabled(false);
        cbCC_V.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbCC_C.setText("CC(C) Carry");
        cbCC_C.setToolTipText("CC Bit 0");
        cbCC_C.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        cbCC_C.setEnabled(false);
        cbCC_C.setMargin(new java.awt.Insets(0, 0, 0, 0));

        lstMem.setToolTipText("A indexed memory dump. The highlighted entry is the current target of the Program Counter.");
        lstMem.setMaximumSize(new java.awt.Dimension(40, 90));
        lstMem.setMinimumSize(new java.awt.Dimension(40, 90));
        jScrollPane2.setViewportView(lstMem);

        lstStack.setToolTipText("The highlighted item is the current target of the Stack Pointer. The items above and below denote the operations space.");
        lstStack.setMaximumSize(new java.awt.Dimension(40, 45));
        lstStack.setMinimumSize(new java.awt.Dimension(40, 45));
        jScrollPane4.setViewportView(lstStack);

        jLabel2.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel2.setText("Memory:");

        jLabel4.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel4.setText("Stack:");

        tpConsole.setEditable(false);
        tpConsole.setFont(new java.awt.Font("Arial", 0, 13));
        tpConsole.setToolTipText("Simulator Output Pane.");
        jScrollPane1.setViewportView(tpConsole);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 13));
        jLabel1.setText("Debug Console:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblRegA)
                    .addComponent(lblRegX)
                    .addComponent(lblRegSP)
                    .addComponent(lblRegCC)
                    .addComponent(jLabel12)
                    .addComponent(lblRegY)
                    .addComponent(lblRegPC)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRegB))
                .addGap(24, 24, 24)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(cbCC_Z)
                    .addComponent(cbCC_S)
                    .addComponent(cbCC_H)
                    .addComponent(cbCC_X)
                    .addComponent(cbCC_I)
                    .addComponent(cbCC_N)
                    .addComponent(cbCC_V)
                    .addComponent(cbCC_C)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(cbCC_S))
                    .addComponent(jLabel12))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRegA)
                            .addComponent(cbCC_X))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbCC_H)
                            .addComponent(lblRegB, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRegX)
                            .addComponent(cbCC_I))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRegY)
                            .addComponent(cbCC_N))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRegPC)
                            .addComponent(cbCC_Z))
                        .addGap(5, 5, 5)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRegSP)
                            .addComponent(cbCC_V))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRegCC)
                            .addComponent(cbCC_C))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)))
                .addContainerGap())
        );

        taskPerformer = new ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
                timerActionPerformed(evt);
            }
  
        };
        
        this.tClock = new Timer (iDelay, taskPerformer);
      
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnBreakPointSet.setText("Set Break Point");
        btnBreakPointSet.setToolTipText("Select memory location from the list to set a break point.\nThe simulator will stop excution when the program counter is pointing at the break point.");
        btnBreakPointSet.setEnabled(false);
        btnBreakPointSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBreakPointSetActionPerformed(evt);
            }
        });

        btnBreakPointRemove.setText("Remove Break Point");
        btnBreakPointRemove.setToolTipText("Select a memory location from the list to remove a break point from the list.");
        btnBreakPointRemove.setEnabled(false);
        btnBreakPointRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBreakPointRemoveActionPerformed(evt);
            }
        });

        btnStackWatchSet.setText("Set Stack Watch");
        btnStackWatchSet.setToolTipText("Select a memory location from the list to add a stack watch point. The simulator will stop if the Stack pointer is pointing at the memory location.");
        btnStackWatchSet.setEnabled(false);
        btnStackWatchSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStackWatchSetActionPerformed(evt);
            }
        });

        btnStackWatchRemove.setText("Remove Stack Watch");
        btnStackWatchRemove.setToolTipText("Select a memory location from the list to remove a Stack Watch point from the list.");
        btnStackWatchRemove.setEnabled(false);
        btnStackWatchRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStackWatchRemoveActionPerformed(evt);
            }
        });

        btnLoadFile.setText("Load File");
        btnLoadFile.setToolTipText("Initialize Simulator and Load a Hex File.");
        btnLoadFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadFileActionPerformed(evt);
            }
        });

        btnSaveConsole.setText("Save Console");
        btnSaveConsole.setToolTipText("Save console output.");
        btnSaveConsole.setEnabled(false);
        btnSaveConsole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveConsoleActionPerformed(evt);
            }
        });

        btnSaveStateDump.setText("Save State Dump");
        btnSaveStateDump.setToolTipText("Save a Dump of Memory.");
        btnSaveStateDump.setEnabled(false);
        btnSaveStateDump.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveStateDumpActionPerformed(evt);
            }
        });

        tfSaveConsole.setEnabled(false);

        tfSaveStateDump.setEnabled(false);

        btnSimReset.setText("Reset");
        btnSimReset.setToolTipText("Select to reinitalize the simulator for repeat excution of simulation.");
        btnSimReset.setEnabled(false);
        btnSimReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimResetActionPerformed(evt);
            }
        });

        btnSimStop.setText("STOP");
        btnSimStop.setToolTipText("Stop running simulation.");
        btnSimStop.setEnabled(false);
        btnSimStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimStopActionPerformed(evt);
            }
        });

        btnSimStepBck.setText("<<STEP");
        btnSimStepBck.setToolTipText("Revert last instruction. (Can only revert one/single instruction cannot revert instructions chains.)");
        btnSimStepBck.setEnabled(false);
        btnSimStepBck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimStepBckActionPerformed(evt);
            }
        });

        btnSimRun.setText("RUN");
        btnSimRun.setToolTipText("Run Simulation in automatic mode.");
        btnSimRun.setEnabled(false);
        btnSimRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimRunActionPerformed(evt);
            }
        });

        btnSimStepFwd.setText("STEP>>");
        btnSimStepFwd.setToolTipText("Process next instruction in memory.");
        btnSimStepFwd.setEnabled(false);
        btnSimStepFwd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimStepFwdActionPerformed(evt);
            }
        });
        //this line is to speed up app development
        this.tfLoadFile.setText("equates.s19");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnStackWatchRemove, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnBreakPointRemove, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnBreakPointSet, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(btnStackWatchSet, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnSimReset, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSimStop, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSimStepBck, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(tfLoadFile, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .addComponent(tfSaveStateDump, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                    .addComponent(tfSaveConsole, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnSimRun, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSimStepFwd, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                    .addComponent(btnSaveStateDump, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addComponent(btnSaveConsole, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                    .addComponent(btnLoadFile, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSimRun)
                            .addComponent(btnSimStepBck)
                            .addComponent(btnSimStop)
                            .addComponent(btnSimStepFwd))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfLoadFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnLoadFile))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfSaveConsole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSaveConsole))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfSaveStateDump, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSaveStateDump)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnBreakPointSet)
                            .addComponent(btnSimReset))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBreakPointRemove)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStackWatchSet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStackWatchRemove)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>                        

    private void btnSimStepFwdActionPerformed(java.awt.event.ActionEvent evt) {                                          
// TODO add your handling code here:
        //Handle first instuction by adding print headers...
        //code to check weather or not to start sim again...
        if(!this.blSimState)
        {
            this.cpuStateCurr.startSimulation();
            this.blSimState = true;
        }
        if(!this.cpuStateCurr.simulate())
        {
            //Handle invalid instruction.
        }
        updateView();    
        
    }                                         

    private void btnSimRunActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        
        if(!this.blSimState)
        {
            this.cpuStateCurr.startSimulation();
            this.blSimState = true;
        }
        
        this.btnSimStop.setEnabled(true);
        this.btnSimRun.setEnabled(false);
        this.btnSimStepFwd.setEnabled(false);
        
        this.tClock.start();
        
        
    }                                         

    private void btnSimStepBckActionPerformed(java.awt.event.ActionEvent evt) {                                          
// TODO add your handling code here:
        
    }                                         

    private void btnSimStopActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        
        this.simStop();
        
    }                                        

    private void btnSimResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        
        if (cpuStateCurr.loadHEXFile())
        {
            
            //code to dum breakpoints and watch points
            this.btnBreakPointSet.setEnabled(true);
            this.btnBreakPointRemove.setEnabled(false);
            this.btnSaveConsole.setEnabled(true);
            this.btnSaveStateDump.setEnabled(true);
            this.btnStackWatchRemove.setEnabled(false);
            this.btnSimRun.setEnabled(true);
            this.btnSimStepFwd.setEnabled(true);
            this.btnStackWatchSet.setEnabled(true);
            
            this.tfSaveConsole.setEnabled(true);
            this.tfSaveStateDump.setEnabled(true);
            
            this.blSimState = false;
            this.initPts();
            updateView();
            
        }      
        
    }         
    
    private void timerActionPerformed(ActionEvent evt)
    {
        if(!this.cpuStateCurr.simulate())
        {
            this.tClock.stop();
            this.cpuStateCurr.stopSimulation();
            
            this.btnSimStop.setEnabled(false);
            
            this.cpuStateCurr.writelnToConsole("Invalid instruction encountered terminiating execution... Reset to restart simulation...");
            
            //this.btnSimStepBck.setEnabled(true);
            
            
        }
        
        
        
        if(this.blBrkPts[this.cpuStateCurr.PC.getUnsignedRegister()])
        {
            
            this.tClock.stop();
            this.cpuStateCurr.writelnToConsole("Break Point ... Stoping simulation.");
            
            this.btnSimStop.setEnabled(false);
            
            this.btnSimRun.setEnabled(true);
            this.btnSimStepFwd.setEnabled(true);
            
        }
        
        if(this.blStkPts[this.cpuStateCurr.SP.getUnsignedRegister()])
        {
            
            this.tClock.stop();
            this.cpuStateCurr.writelnToConsole("Stack Point ... Stoping simulation.");
            
            this.btnSimStop.setEnabled(false);
            
            this.btnSimRun.setEnabled(true);
            this.btnSimStepFwd.setEnabled(true);
            
        }
        
        this.updateView();

    }

    
    private void simStop()
    {
        
        this.btnSimStop.setEnabled(false);
        this.btnSimRun.setEnabled(true);
        this.btnSimStepFwd.setEnabled(true);
        //this.btnSimStepBck.setEnabled(true);  
        
        this.tClock.stop();
        
    }
    

    private void btnSaveStateDumpActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        try {
            
        this.cpuStateCurr.printState();
        this.cpuStateCurr.printMemoryDump();
        BufferedWriter out = new BufferedWriter(new FileWriter(this.tfSaveStateDump.getText()));
        out.write(this.tpConsole.getText());
        out.close();
        
            this.cpuStateCurr.writelnToConsole("File Written...");;
        }
        catch (IOException e) {
            
            this.cpuStateCurr.writelnToConsole("Unable to write to file...");
            
        }
    
    }                                        

    private void btnSaveConsoleActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        
        try {
            
        BufferedWriter out = new BufferedWriter(new FileWriter(this.tfSaveConsole.getText()));
        out.write(this.tpConsole.getText());
        out.close();
        
            this.cpuStateCurr.writelnToConsole("File Written...");;
        }
        catch (IOException e) {
            
            this.cpuStateCurr.writelnToConsole("Unable to write to file...");
            
        }
    }                                        

    private void btnLoadFileActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        this.cpuStateCurr = new M9S12();
        this.cpuStateCurr.setConsole(this.tpConsole);
        
        this.blBrkPts = new boolean[this.cpuStateCurr.iMemSize];
            
        this.blStkPts = new boolean[this.cpuStateCurr.iMemSize];
            
        initPts();
        
        
        if(this.cpuStateCurr.loadHEXFile(this.tfLoadFile.getText()))//successful load
        {            
            updateView();
            this.btnBreakPointSet.setEnabled(true);
            this.btnSaveConsole.setEnabled(true);
            this.btnSaveStateDump.setEnabled(true);
            this.btnSimReset.setEnabled(true);
            this.btnSimRun.setEnabled(true);
            this.btnSimStepFwd.setEnabled(true);
            this.btnStackWatchSet.setEnabled(true);
            
            this.tfSaveConsole.setEnabled(true);
            this.tfSaveStateDump.setEnabled(true);
            this.blSimState=false;
            

        }
        
 
    }                                        

    private void btnStackWatchRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        int indexes[] = this.lstStack.getSelectedIndices();
        
        for (int i = 0; i < indexes.length; i++)
        {
            this.blStkPts[indexes[i]] = false;
        }
        this.updateView();
    }                                        

    private void btnStackWatchSetActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        this.btnStackWatchRemove.setEnabled(true);
        int indexes[] = this.lstStack.getSelectedIndices();
        
        for (int i = 0; i < indexes.length; i++)
        {
            this.blStkPts[indexes[i]] = true;
        }
        
        this.updateView();
    }                                        

    private void btnBreakPointRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        
        int indexes[] = this.lstMem.getSelectedIndices();
        
        for (int i = 0; i < indexes.length; i++)
        {
            this.blBrkPts[indexes[i]] = false;
        }
        this.updateView();
    }                                        

    private void btnBreakPointSetActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        this.btnBreakPointRemove.setEnabled(true);
        
        int indexes[] = this.lstMem.getSelectedIndices();
        
        for (int i = 0; i < indexes.length; i++)
        {
            this.blBrkPts[indexes[i]] = true;
        }
        
        this.updateView();
        
    }
    
    private void updateView()    {
        
        lblRegA.setText("A:  " + this.cpuStateCurr.A.getHex() + " (" + this.cpuStateCurr.A.getUnsignedRegister()+")");
        lblRegB.setText("B:  " + this.cpuStateCurr.B.getHex() + " (" + this.cpuStateCurr.B.getUnsignedRegister()+")");
        
        lblRegX.setText("X:  " + this.cpuStateCurr.X.getHex() + " (" + this.cpuStateCurr.X.getUnsignedRegister()+")");
        lblRegY.setText("Y:  " + this.cpuStateCurr.Y.getHex() + " (" + this.cpuStateCurr.Y.getUnsignedRegister()+")");
        
        
        lblRegPC.setText("PC: " + this.cpuStateCurr.PC.getHex() + " (" + this.cpuStateCurr.PC.getUnsignedRegister()+")");
        lblRegSP.setText("SP: " + this.cpuStateCurr.SP.getHex() + " (" + this.cpuStateCurr.SP.getUnsignedRegister()+")");        
        
        lstMem.setModel(new javax.swing.AbstractListModel() {
           String[] strings = cpuStateCurr.getMemory(blBrkPts);           
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        
        
        
       
        
        lstStack.setModel(new javax.swing.AbstractListModel() {
           String[] strings = cpuStateCurr.getStack(blStkPts);
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        

           
 

    }
    
    private void initPts()
    {
        for (int i = 0; i < this.blBrkPts.length; i++)
        {
            this.blBrkPts[i] = false;
            this.blStkPts[i] = false;
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    
    private M9S12 cpuStateCurr;
    //private M9S12 cpuStateLast;
    
    int iDelay = 1000; //1000 milliseconds clock time.
    
    private javax.swing.JButton btnBreakPointSet;
    private javax.swing.JButton btnSimStepBck;
    private javax.swing.JButton btnSimRun;
    private javax.swing.JButton btnSimStepFwd;
    private javax.swing.JButton btnBreakPointRemove;
    private javax.swing.JButton btnStackWatchSet;
    private javax.swing.JButton btnStackWatchRemove;
    private javax.swing.JButton btnLoadFile;
    private javax.swing.JButton btnSaveConsole;
    private javax.swing.JButton btnSaveStateDump;
    private javax.swing.JButton btnSimReset;
    private javax.swing.JButton btnSimStop;
    
    private javax.swing.JCheckBox cbCC_S;
    private javax.swing.JCheckBox cbCC_X;
    private javax.swing.JCheckBox cbCC_H;
    private javax.swing.JCheckBox cbCC_I;
    private javax.swing.JCheckBox cbCC_N;
    private javax.swing.JCheckBox cbCC_Z;
    private javax.swing.JCheckBox cbCC_V;
    private javax.swing.JCheckBox cbCC_C;
    
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblRegSP;
    private javax.swing.JLabel lblRegCC;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblRegA;
    private javax.swing.JLabel lblRegB;
    private javax.swing.JLabel lblRegX;
    private javax.swing.JLabel lblRegY;
    private javax.swing.JLabel lblRegPC;
    
    private javax.swing.JList lstMem;
    private javax.swing.JList lstStack;
    
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    
    private javax.swing.JTextField tfLoadFile;
    private javax.swing.JTextField tfSaveConsole;
    private javax.swing.JTextField tfSaveStateDump;
    
    private javax.swing.JTextPane tpConsole;
    
    private javax.swing.Timer tClock;
    
    private boolean blSimState;

    ActionListener taskPerformer;
    
    private boolean [] blBrkPts;
    private boolean [] blStkPts;
    
}
